package com.example.myyunpan.Controller;

import com.example.myyunpan.annotation.GlobalIntercepter;
import com.example.myyunpan.annotation.VerifyParam;
import com.example.myyunpan.entity.config.AppConfig;
import com.example.myyunpan.entity.constants.Constants;
import com.example.myyunpan.entity.dto.SessionWebUserDto;
import com.example.myyunpan.entity.enums.FileCategoryEnums;
import com.example.myyunpan.entity.enums.FileDelFlagEnums;
import com.example.myyunpan.entity.query.FileInfoQuery;
import com.example.myyunpan.entity.vo.FileInfoVO;
import com.example.myyunpan.entity.vo.PaginationResultVO;
import com.example.myyunpan.entity.vo.ResponseVO;
import com.example.myyunpan.service.FileInfoService;
import com.example.myyunpan.utils.MyFileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/recycle")
public class RecycleController {

    @Autowired
    private FileInfoService fileInfoService;

    @Autowired
    public MyFileUtils myFileUtils;

    @Autowired
    private AppConfig appConfig;


    /**
     * 根据条件分页查询
     */
    @RequestMapping("/loadRecycleList")
    @GlobalIntercepter(checkParams = true)
    public ResponseVO loadRecycleList(HttpSession session, Integer pageNo,Integer pageSize){
        SessionWebUserDto webUserDto = (SessionWebUserDto) session.getAttribute(Constants.SESSION_KEY);
        FileInfoQuery query = new FileInfoQuery();
        query.setPageSize(pageSize);
        query.setPageNo(pageNo);
        query.setUserId(webUserDto.getUserId());
        query.setOrderBy("recovery_time desc");
        query.setDelFlag(FileDelFlagEnums.RECYCLE.getFlag());
        PaginationResultVO resultVO = fileInfoService.queryPageInfo(query);
        return ResponseVO.success(myFileUtils.convert2PaginationVO(resultVO, FileInfoVO.class));
    }

    @RequestMapping("/recoverFile")
    @GlobalIntercepter(checkParams = true)
    public ResponseVO recoverFile(HttpSession session, @VerifyParam(required = true) String fileIds){
        SessionWebUserDto webUserDto = (SessionWebUserDto) session.getAttribute(Constants.SESSION_KEY);
        fileInfoService.recoverFileBatch(webUserDto.getUserId(), fileIds);
        return ResponseVO.success();
    }

    /**
     * 彻底删除
     * @param session
     * @param fileIds
     * @return
     */
    @RequestMapping("/delFile")
    @GlobalIntercepter(checkParams = true)
    public ResponseVO delFile(HttpSession session, @VerifyParam(required = true) String fileIds){
        SessionWebUserDto webUserDto = (SessionWebUserDto) session.getAttribute(Constants.SESSION_KEY);
        fileInfoService.delFileBatch(webUserDto.getUserId(), fileIds, false);
        return ResponseVO.success();
    }


}
